<template>
	<view class="container">
		<uni-search-bar placeholder="输入名称搜索" v-model="search" @confirm="searchClick" @cancel="searchClear" @clear="searchClear"></uni-search-bar>
		<view class="grid" v-if="getProducts.length>0">
			<view class="ver-layout-center" v-for="(item,index) in getProducts" :key="index" @click="toDetail(item)">
        <image v-if="item.image.length" :src="item.image[0]" class="image-view" mode="aspectFill"></image>
        <view v-else class="image-view">
          暂无图片
        </view>
				<view class="title-view">{{item.title}}</view>
			</view>
		</view>
    <view v-else class="list-no-data">暂无数据</view>
		<view @click="createProduct" class="create-product" hover-class="active" hover-stay-time="100">新建产品</view>
	</view>
</template>

<script>
	import uniSearchBar from '@/components/uni-search-bar/uni-search-bar'
  import {create,osslist} from "@/router.js"
	export default {
		components: {
			uniSearchBar
		},
		data() {
			return {
				search: '',
				searchKeys: ['title'], //search查询的字段
				products: [],
				searchArr: []
			}
		},
		computed: {
			getProducts() {
				if (this.searchArr && this.searchArr.length > 0) return this.searchArr
				else return this.products
			}
		},
		mounted() {
			this.getProductList()
		},
    onNavigationBarButtonTap(params) {
      this.$tools.pageGo(osslist)
    },
		methods: {
			searchClear() {
				this.searchArr = []
			},
			searchClick() {
				uniCloud.callFunction({
					name: 'database',
					data: {
						collection: 'product',
						like: {
							keys: this.searchKeys,
							value: this.search
						}
					}
				}).then(res => {
					if (res.result.affectedDocs) this.searchArr = res.result.data
          else this.searchArr=[]
				})
			},
			toDetail(item) {
				let id = item._id
				uni.navigateTo({
					url: `./detail?id=${id}`
				})
			},
			getProductList() {
				this.$tools.showLoading()
				uniCloud.callFunction({
					name: 'database',
					data: {
						collection: 'product'
					}
				}).then(res => {
          uni.hideLoading()
					if (res.result.affectedDocs) this.products = res.result.data
          else this.products = []
				})
			},
			createProduct() {
				uni.navigateTo({ url: create })
			}
		}
	}
</script>

<style>
	.grid {
		margin-top: 10px;
		display: grid;
		width: calc(100% - 30px);
		grid-template-columns: repeat(3, 33%);
		/* 相当于 1fr 1fr 1fr */
		/* grid-template-rows: repeat(3, 1fr); */
		/* fr单位可以将容器分为几等份 */
		grid-gap: 1%;
		/* grid-column-gap 和 grid-row-gap的简写 */
		grid-auto-flow: row;
	}

	.image-view {
		width: 80px;
		height: 80px;
    color: #aaaaaa;
    line-height: 80px;
    text-align: center;
    font-size: 12px;
		margin: auto;
    border: 1px solid #eaeaea;
	}

	.title-view {
		width: 100%;
		margin: 5px;
		text-align: center;
		font-size: small;
		white-space: nowrap;
		overflow: hidden;
		text-overflow: ellipsis;
	}

	.create-product {
		width: 80%;
		left: 10%;
		position: fixed;
		bottom: 10px;
		color: white;
		height: 35px;
		font-size: small;
		line-height: 35px;
		text-align: center;
		border-radius: 5px;
		background: #007AFF;
	}

	.active {
		background: #0064d0;
	}
</style>
